package com.lyl.serviceedu.controller.api;

import com.lyl.commonutils.DateUtils;
import com.lyl.commonutils.R;
import com.lyl.commonutils.redis.RedisConstants;
import com.lyl.commonutils.redis.RedisUtils;
import com.lyl.serviceedu.entity.Course;
import com.lyl.serviceedu.entity.EduTeacher;
import com.lyl.serviceedu.service.CourseService;
import com.lyl.serviceedu.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/serviceedu/index")
public class IndexController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //查询前8条热门课程，查询前4条名师
    @GetMapping
    public R index() {

        // 首页名师与课程默认存储在0号数据库
        RedisUtils.setDataBase(redisTemplate,0);
        // 前8条课程信息
        List<Course> courseList = courseService.listIndexCourse();

        // 前4个名师
        List<EduTeacher> teacherList = teacherService.listIndexTeacher();

        // 首页访问量加1 信息在4号数据库
        RedisUtils.setDataBase(redisTemplate,4);
        // key为当前日期的登录量
        RedisUtils.incr(redisTemplate, DateUtils.formatDate(new Date(),0)+"/"+ RedisConstants.WEB_ACCESS_NUM,1);

        return R.ok().data("courseList",courseList).data("teacherList",teacherList);
    }
}